#include "TT_Logic_Playing.h"

using namespace TT_Logic;

Playing_State::Playing_State() { //The constructor

	P1_Cx = 0,P1_Cy = -350,P1_Cz = 100,P1_ID = 1;
//	P2_Cx = 0,P2_Cy = -300,P2_Cz = 100,P2_ID = 1;
	Arena_Row = 801,Arena_Col = 801;

	Round = new TT_Core::Arena(Arena_Row,Arena_Col);

}

void Playing_State::Init() {

	Round_Anima_ID = Anima.AddObject((void (*)(void *))Round -> IdleMoveReal,Round);
	P1_EyeDir_Anima_ID = Anima.AddObject((void (*)(void *))P1_EyeDir.IdleMoveReal,&P1_EyeDir);
	NewRound();
}

void Playing_State::Exit() {

	Anima.SendSignal(OBJ_STATE_EXIT,Round_Anima_ID);
	Anima.SendSignal(OBJ_STATE_EXIT,P1_EyeDir_Anima_ID);
}

void Playing_State::EyeDeg::Change(int ActType) {

	if (ActType == 1) Des = Des - 90; 
	else Des = Des + 90; 
}

void Playing_State::EyeDeg::IdleMoveReal(EyeDeg *Real) {

	float &Deg = Real -> Deg,&Des = Real -> Des,&d = Real -> d;
	if (fabs(Deg - Des) > EYE_ROTATE_EPS) 
	{
		if (Deg > Des) Deg -= 0.03;
		else Deg += 0.03;
	}
	else Deg = Des; //fix
}

void Playing_State::NewRound() {

	Round -> InitMatch();
	Round -> AddPlayer(0,250,2,  10,0,125,0);
	for (int i = 1; i <= 50 ; i++)
		Round -> AddPlayer(rand() % Arena_Row,rand() % Arena_Col,rand() % 4,10,rand() % 127 + 128,rand() % 127 + 128,rand() % 127 + 128);
	P1_Ptr = Round -> GetPlayer(P1_ID);

	P1_EyeDir.Deg = 0;
	P1_EyeDir.Des = 0;

}


void Playing_State::Idle(double DetTime) {

	if (!(rand() % 500))
		for (const TT_Core::PlayerNode *ptr = Round -> GetPlayerIterator(); ptr; ptr = ptr -> next)
			if (ptr != P1_Ptr)
			{
				Round -> FlushPlayer(ptr -> ID,rand() % 2);
			}
	Anima.MoveOn(DetTime);
	for (const TT_Core::PlayerNode *ptr = Round -> GetPlayerIterator(); ptr;)
	{
		const TT_Core::PlayerNode *nptr = ptr -> next;
		if (ptr && ptr -> IsDead && ptr -> Alpha < 1e-8) 
			Round -> DelPlayer(ptr -> ID);
		ptr = nptr;

	}
}

void Playing_State::Draw() {

	if (!P1_Ptr -> IsDead) P1.x = P1_Ptr -> x, P1.y = P1_Ptr -> y;
	TT_Graphics::Camera_Playing(P1,P1_Cx,P1_Cy,P1_Cz,P1_EyeDir.Deg);

	TT_Graphics::Draw_Arena(Arena_Row,Arena_Col);

	for (const TT_Core::PlayerNode *ptr = Round -> GetPlayerIterator(); ptr; ptr = ptr -> next)
		TT_Graphics::Draw_Player_Quad(ptr);

	for (const TT_Core::PlayerNode *ptr = Round -> GetPlayerIterator(); ptr; ptr = ptr -> next)
		TT_Graphics::Draw_Player_DownOutline(ptr);

	for (const TT_Core::PlayerNode *ptr = Round -> GetPlayerIterator(); ptr; ptr = ptr -> next)
		TT_Graphics::Draw_Player_UpOutline(ptr);

	for (const TT_Core::PlayerNode *ptr = Round -> GetPlayerIterator(); ptr; ptr = ptr -> next)
		TT_Graphics::Draw_Player_Vertical(ptr);

}

void Playing_State::Special_Keyboard_Trigger(int key,int x,int y) {

	switch (key)
	{
		case GLUT_KEY_LEFT :

			Round -> FlushPlayer(P1_ID,ACT_TYPE_LEFT);
			P1_EyeDir.Change(0);
			break;

		case GLUT_KEY_RIGHT :

			Round -> FlushPlayer(P1_ID,ACT_TYPE_RIGHT);
			P1_EyeDir.Change(1);
			break;

		case GLUT_KEY_F1 :

			Round -> FlushPlayer(P1_ID,ACT_TYPE_SPEEDUP);
			break;

		case GLUT_KEY_F2 :

			Round -> FlushPlayer(P1_ID,ACT_TYPE_SPEEDDOWN);
			break;

		case GLUT_KEY_F3 :

			NewRound();
			break;
	}
}	

void Playing_State::Keyboard_Trigger(UCHAR key,int x,int y) {

	if (key == ' ') 
	{
	}
	else if (key == 27)/*Escape*/ exit(0);
	else if (key == 'f') Anima.SendSignal(OBJ_STATE_SLEEPING,2);
	else if (key == 'b') Anima.SendSignal(OBJ_STATE_RUNNING,2);
}

void Playing_State::Mouse_Trigger() {
}

